﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;

using ATMCommonLib;

namespace ATMEncryption
{
    public class MessageAuthenticationCodeSHA1
    {
        public static void EncryptBytes(byte[] HaKey, byte[] HaData, out string hash)
        {
            HMACSHA1 Hmac = new HMACSHA1(HaKey);

            byte[] bArray = Hmac.ComputeHash(HaData);

            hash = ATMCommonLib.EncodingHelper.BytesToStr(bArray);
        }

        public static void EncryptBytes(byte[] HaKey, byte[] HaData, out byte[] hash)
        {
            HMACSHA1 Hmac = new HMACSHA1(HaKey);

            byte[] bArray = Hmac.ComputeHash(HaData);

            hash = bArray;
        }

        public static bool ValidateBytes(byte[] HaKey, byte[] HaData, string hash)
        {
            if (string.IsNullOrEmpty(hash))
            {
                return false;
            }

            HMACSHA1 Hmac = new HMACSHA1(HaKey);

            byte[] bArray = Hmac.ComputeHash(HaData);

            string hashComp = ATMCommonLib.EncodingHelper.BytesToStr(bArray);

            if (!string.IsNullOrEmpty(hashComp) && hashComp.Equals(hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ValidateBytes(byte[] HaKey, byte[] HaData, byte[] hash)
        {
            string hashStr = ATMCommonLib.EncodingHelper.BytesToStr(hash);

            return ValidateBytes(HaKey, HaData, hash);
        }
    }
}
